/*
 * $Id: CategoryProgrammeAttributeServiceImpl.java 1700 2011-08-29 11:42:38Z abie $
 * 
 * Copyright (c) 2001-2008 Accentrix Company Limited. All Rights Reserved.
 */
package com.lightsaber.trade.cloth.service.category.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.lightsaber.trade.cloth.dao.category.CategoryProgrammeAttributeDao;
import com.lightsaber.trade.cloth.entity.category.CategoryLeaf;
import com.lightsaber.trade.cloth.entity.category.CategoryProgrammeAttribute;
import com.lightsaber.trade.cloth.service.category.CategoryProgrammeAttributeService;

@Component
@Transactional
public class CategoryProgrammeAttributeServiceImpl implements CategoryProgrammeAttributeService {

    @Autowired
    CategoryProgrammeAttributeDao attDao;

    @Override
    public List<CategoryProgrammeAttribute> findByCategoryNodeId(final String categoryNodeId) {
        return attDao.findByCategoryNodeId(categoryNodeId);
    }

    @Override
    public void deleteByCategoryNodeId(final String categoryNodeId) {
        attDao.deleteByCategoryNodeId(categoryNodeId);
    }

    @Override
    public void save(final CategoryProgrammeAttribute categoryProgrammeAttribute) {
        if (StringUtils.isBlank(categoryProgrammeAttribute.getId())) {
            // CategoryProgrammeAttribute newAtt = new
            // CategoryProgrammeAttribute();
            // newAtt.setProgrammeAttribute(categoryProgrammeAttribute.getProgrammeAttribute());
            // newAtt.setValue(categoryProgrammeAttribute.getValue());
            // newAtt.setCategoryLeaf(categoryProgrammeAttribute.getCategoryLeaf());
            attDao.save(categoryProgrammeAttribute);
        } else {
            CategoryProgrammeAttribute old = findById(categoryProgrammeAttribute.getId());
            old.setProgrammeAttribute(categoryProgrammeAttribute.getProgrammeAttribute());
            old.setValue(categoryProgrammeAttribute.getValue());
            attDao.update(old);
        }
    }

    @Override
    public void deleteMultitude(final List<String> delAttrs) {
        if (delAttrs != null) {
            for (String delAttr : delAttrs) {
                attDao.delete(delAttr);
            }
        }
    }

    @Override
    public CategoryProgrammeAttribute findByFundingMode(final CategoryLeaf leaf) {
        return attDao.findByFundingMode(leaf);
    }

    @Override
    public CategoryProgrammeAttribute findById(final String attrId) {
        return attDao.findUniqueBy("id", attrId);
    }

    @Override
    public void copyAttributeByCategoryNode(final CategoryLeaf oldNode, final CategoryLeaf newNode) {
        List<CategoryProgrammeAttribute> cpaList = oldNode.getCategoryProgrammeAttributes();
        if (cpaList.isEmpty()) {
            return;
        }
        for (CategoryProgrammeAttribute old : cpaList) {
            CategoryProgrammeAttribute newCpa = new CategoryProgrammeAttribute();
            newCpa.setCategoryLeaf(newNode);
            newCpa.setProgrammeAttribute(old.getProgrammeAttribute());
            newCpa.setValue(old.getValue());
            save(newCpa);
        }
    }

}
